package dtd.to.xmlschema;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Miro
 */
public class TextParser {

    /**
     * Vycisti text od zbytocnych bielych znakov.
     *
     * @param text text na vycistenie
     * @return vycisteny text
     */
    public static String cleanText(String text) {
        text = text.replace('\n', ' ');
        text = text.replace('\t', ' ');
        text = text.replaceAll(" ", "");
        text = text.trim();

        return text;
    }

    /**
     * Odstrani prvy a posledny znak v retazci, ak su to parove zatvorky
     * zatvorky
     *
     * @param text
     * @return
     */
    public static String cleanBrackets(String text) {
        if (text.startsWith("(") && text.endsWith(")")
                && findPairBracket(text, null) == (text.length() - 1)) {

            return text.substring(1, text.length() - 1);
        }

        return text;
    }

    /**
     * Najde par k zatvorke. Zatvorky mozu mat vnorene zatvorky. Hlada par k
     * prvej najdenej zatvorke. Pri zadani startovej pozicie zacina hladanie na
     * tejto pozicii.
     *
     * @param text text so zatvorkami
     * @return index zatvorky
     */
    public static int findPairBracket(String text, Integer startPossition) {
        int count = 0;
        int possition = 0;
        boolean found = false;

        if (startPossition != null) {
            possition = startPossition;
        }

        while (possition < text.length()) {
            char ch = text.charAt(possition);
            if (ch == '(') {
                count++;
                found = true;
            } else if (ch == ')') {
                count--;
            }

            if (count == 0 && found == true) {
                return possition;
            }

            possition++;
        }

        throw new IllegalArgumentException();
    }

    /**
     * Najde oddelovac ('|' alebo ',') v zadanom vyraze
     *
     * @param text
     * @return oddelovac
     * @throws IllegalArgumentException ak zadany retazec neobsahuje oddelovac
     * (okrem zatvoriek)
     */
    public static char getSeparator(String text) {
        int possition = 0;

        while (possition <= text.length()) {
            char ch = text.charAt(possition);
            if (ch == '(') {
                possition = findPairBracket(text, possition);
                continue;
            } else if (ch == '|') {
                return '|';
            } else if (ch == ',') {
                return ',';
            }

            possition++;
        }

        throw new IllegalArgumentException();
    }

    /**
     * Rozdeli vstupny text na kusky podla zadaneho vzoru
     *
     * @param text taxt na rozdelenie
     * @param pattern vzor
     * @param chops
     * @return
     */
    public static List<String> chopText(String text, String pattern) {

        if (text.length() == 0) {
            return null;
        }

        List<String> chops = new LinkedList<String>();

        while (text.length() > 0) {

            int endOfElementIndex;
            if (text.indexOf("(") == 0) {
                endOfElementIndex = findPairBracket(text, null) + 1;
            } else {
                endOfElementIndex = text.indexOf(pattern);
            }

            if (endOfElementIndex == -1) {
                chops.add(text);
                return chops;
            } else {
                String chop = text.substring(0, endOfElementIndex);
                chops.add(chop);

                if (endOfElementIndex + 1 > text.length()) {
                    return chops;
                }
                text = text.substring(endOfElementIndex + 1);

            }
        }
        return chops;
    }

    /**
     * Parser na rozdelenie subelementov do pola, prvy retazec je spojovaci znak
     * ("|" alebo ",")
     *
     * @param text skupina elementov
     * @return subelementy alebo ich mnoziny
     */
    public static List<String> parseGroup(String text) {
        List<String> chunks = new LinkedList<String>();

        int startBracketIndex = text.indexOf("(");
        int endBracketIndex = text.lastIndexOf(")");

        String withoutBracket;
        if (startBracketIndex != -1) {
            withoutBracket = (text.substring(0, startBracketIndex)).concat(text.substring(endBracketIndex + 1));
        } else {
            withoutBracket = text;
        }

        if (withoutBracket.contains("|")) {
            chunks.add("|");
            chunks.addAll(chopText(text, "|"));
        } else if (withoutBracket.contains(",")) {
            chunks.add(",");
            chunks.addAll(chopText(text, ","));
        }

        return chunks;
    }

    /**
     * Vrati znak multiplicity na konci retazca, ak nie je, vrati ' '
     *
     * @param text
     * @return
     */
    public static char getMutliplicity(String text) {
        if (text.endsWith("*")
                || text.endsWith("+")
                || text.endsWith("?")) {
            return text.charAt(text.length() - 1);
        } else {
            return ' ';
        }
    }
}
